﻿using ShiQuan.DataRepository;
using ShiQuan.Helper;
using ShiQuan.IotDevice.Business;
using ShiQuan.IotDevice.Entity;
using ShiQuan.Utility;
using ShiQuan.WebBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace ShiQuan.IotDevice.Web.Controllers
{
    /// <summary>
    /// 联网设备WebApi
    /// </summary>
    public class IotDeviceDataController : BaseApiController
    {
        // GET: api/IotDeviceData
        /// <summary>
        /// 获取服务器时间
        /// </summary>
        /// <returns></returns>
        public AjaxResult GetTime()
        {
            return this.Success("读取成功",DateTime.Now.ToString());
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public AjaxResult SaveData()
        {
            try
            {
                HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
                HttpRequestBase requestBase = context.Request;//定义传统request对象  
                var source = requestBase["entity"];

                IotDeviceDataBLL service = new IotDeviceDataBLL();
                service.SaveData(source);

                return this.Success("上传成功");
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
        }

        /// <summary>
        /// 读取设备最新数据 
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns>返回对象Json</returns>
        [HttpGet]
        public AjaxResult GetDeviceLast(string serialNumber)
        {
            try
            {
                IotDeviceInstallBLL service = new IotDeviceInstallBLL();
                /*设备安装信息*/
                var deviceInstall = service.FindEntity(my => my.SerialNumber == serialNumber);
                if (deviceInstall == null)
                    return this.Warning("未能找到设备安装信息!");
                /*设备最新数据*/
                IotDeviceLastEntity deviceLast = new IotDeviceLastBLL().GetEntity(deviceInstall.Id);
                if (deviceLast == null)
                    return this.Warning("此设备还未收到最新数据！");

                var jsonData = new
                {
                    SerialNumber = deviceInstall.SerialNumber,
                    InstalledAddr = deviceInstall.InstalledAddr,
                    DateTime=deviceLast.DateTime,
                    OC01Value = deviceLast.OC01Value,
                    OC01Text = deviceLast.OC01Text,
                    OC02Value = deviceLast.OC02Value,
                    OC02Text = deviceLast.OC02Text,
                };
                return Success("读取成功", jsonData.ToJson());
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">查询参数</param>
        /// <returns>返回分页列表Json</returns>
        [HttpGet]
        public AjaxResult GetPageList(int page, int rows, string queryJson)
        {
            try
            {
                Pagination pagination = new Pagination();
                pagination.rows = rows;
                pagination.page = page;

                IotDeviceDataBLL service = new IotDeviceDataBLL();
                var watch = CommonHelper.TimerStart();
                var data = service.GetPageList(pagination, queryJson);
                var jsonData = new
                {
                    rows = data,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records,
                    costtime = CommonHelper.TimerEnd(watch)
                };
                return Success("读取成功", jsonData);
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
            
        }
    } 
}
